package com.cda.pes.service.impl;

import com.cda.pes.VO.FavouriteVO;
import com.cda.pes.mapper.FavouriteMapper;
import com.cda.pes.mapper.UserMapper;
import com.cda.pes.pojo.Favourite;
import com.cda.pes.pojo.User;
import com.cda.pes.service.IFavouriteService;
import com.cda.pes.service.IUserService;
import com.cda.pes.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 用户模块业务层的实现类
 */
@Service
public class FavouriteServiceImpl implements IFavouriteService {

    @Autowired
    private FavouriteMapper favouriteMapper;

//    @Override
//    public Integer insert(Favourite favourite) {
//        Integer result = favouriteMapper.findByPidAndUid(favourite.getPid(), favourite.getUid());
//        if (result == 1){
//            throw new InsertException("你已经收藏过该商品了");
//        }
//        Integer rows = favouriteMapper.insert(favourite);
//        if (rows!=1){
//            throw new InsertException("收藏商品时发生未知异常");
//        }
//        return rows;
//    }

    @Override
    public void insert(Favourite favourite) {
        Integer pid = favourite.getPid();
        Integer uid = favourite.getUid();
        Integer result = favouriteMapper.findByPidAndUid(pid,uid);
        if (result == 1){
            throw new InsertException("你已经收藏过该商品了");
        }
        Integer rows = favouriteMapper.insert(favourite);
        if (rows!=1){
            throw new InsertException("收藏商品时发生未知异常");
        }
    }

    @Override
    public void deleteFavourite(Integer fid) {
        Integer result = favouriteMapper.findByFid(fid);
        if (result == 1){
//            throw new FavouriteNotFoundException("未找到该商品的收藏");
            System.out.println("该商品在收藏里");
        }
        Integer rows = favouriteMapper.deleteFavourite(fid);
        if (rows != 1){
            throw new FavouriteNotFoundException("取消收藏时发生未知错误");
        }
    }

    @Override
    public List<FavouriteVO> findVOByUid(Integer uid) {
        List<FavouriteVO> list = favouriteMapper.findVOByUid(uid);
        return list;
    }

    @Override
    public Integer findByPidAndUid(Integer pid, Integer uid) {
        Integer result = favouriteMapper.findByPidAndUid(pid, uid);
//        if (result != 1){
//            throw new FavouriteNotFoundException("查询的商品未被收藏");
//        }
        return result;
    }

    @Override
    public Integer findByFid(Integer fid) {
        Integer rows = favouriteMapper.findByFid(fid);
        return rows;
    }

}
